﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace NutriFast
{
    public partial class UsuarioAdd : Form
    {
        public UsuarioAdd()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Survey sry = new Survey();
            //OPEN FILE HERE
            string file = "../../Survey/HC.txt";

            if (System.IO.File.Exists(file) == true)
            {
                System.IO.StreamReader objReader;
                objReader = new System.IO.StreamReader(file);

                sry.textBox1.Text = objReader.ReadToEnd();
                objReader.Close();
                sry.ShowDialog();
            }
            else
            {
                //ERROR MESSAGE HERE
                MessageBox.Show(this, "No se encuentra el archivo", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        WebCam webcam;
        private void UsuarioAdd_Load(object sender, EventArgs e)
        {
            webcam = new WebCam();
            webcam.InitializeWebCam(ref imgVideo);
        }

        private void button2_Click(object sender, EventArgs e)
        {

            if (this.pname.Text == "" || this.sex.SelectedItem == null ||
                this.hw.Text == "" || this.cw.Text == "" || this.pct.Text == "" ||
                this.pcb.Text == "" || this.pcse.Text == "" || this.cbrazo.Text == "" ||
                this.ccint.Text == "" || this.ccad.Text == "" || this.cabd.Text == "" ||
                this.cmun.Text == "" || this.hcod.Text == "" || this.ccod.Text == "" ||
                this.pcsi.Text == "")
            {

                MessageBox.Show(this, "No debe de haber campos vacios", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

                if (this.pname.Text == "")
                    errorProvider1.SetError(this.pname,"Obligatorio");
                else if (this.sex.SelectedItem == null)
                    errorProvider1.SetError(this.sex, "Obligatorio");
                else if (this.hw.Text == "")
                    errorProvider1.SetError(this.hw, "Obligatorio");
                else if (this.cw.Text == "")
                    errorProvider1.SetError(this.cw, "Obligatorio");
                else if (this.pct.Text == "")
                    errorProvider1.SetError(this.pct, "Obligatorio");
                else if (this.pcb.Text == "")
                    errorProvider1.SetError(this.pcb, "Obligatorio");
                else if (this.ccint.Text == "")
                    errorProvider1.SetError(this.ccint, "Obligatorio");
                else if (this.ccad.Text == "")
                    errorProvider1.SetError(this.ccad, "Obligatorio");
                else if (this.cabd.Text == "")
                    errorProvider1.SetError(this.cabd, "Obligatorio");
                else if (this.cmun.Text == "")
                    errorProvider1.SetError(this.cmun, "Obligatorio");
                else if (this.hcod.Text == "")
                    errorProvider1.SetError(this.hcod, "Obligatorio");
                else if (this.ccod.Text == "")
                    errorProvider1.SetError(this.ccod, "Obligatorio");
                else if (this.pcsi.Text == "")
                    errorProvider1.SetError(this.pcsi, "Obligatorio");
                else if (this.pcse.Text == "")
                    errorProvider1.SetError(this.pcse, "Obligatorio");
                else if(this.cbrazo.Text == "")
                    errorProvider1.SetError(this.cbrazo, "Obligatorio");

            }
            else
            {
                Patient pt= new Patient(this.pname.Text, this.bd.Value.Date, "" + this.sex.SelectedItem,
                    float.Parse(this.hw.Text), float.Parse(this.cw.Text), (float)this.he.Value, float.Parse(this.pct.Text), float.Parse(this.pcb.Text),
                    float.Parse(this.pcse.Text), float.Parse(this.cbrazo.Text), float.Parse(this.ccint.Text), float.Parse(this.ccad.Text), float.Parse(this.cabd.Text),
                    float.Parse(this.cmun.Text), float.Parse(this.hcod.Text), "", float.Parse(this.ccod.Text), float.Parse(this.pcsi.Text));
                
                //Insertar Paciente
                if (dbVic.insertarPaciente(pt))
                {

                    //Message
                    MessageBox.Show(this, "Paciente Agregado", "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    this.Close();
                }
                else
                {
                    MessageBox.Show(this, "Error al agregar al Paciente", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

                }//Fin del else
            }//Fin de validacion de campos vacios

        }

        private void pname_Validating(object sender, CancelEventArgs e)
        {
            int count = pname.Text.Length;

        }

        private void cw_Validating(object sender, CancelEventArgs e)
        {
            if (this.cw.Text != "")
                errorProvider1.SetError(this.cw, "");
         
        }

        private void sex_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.sex.SelectedItem != null)
                errorProvider1.SetError(this.sex, "");
        }

        private void pct_Validating(object sender, CancelEventArgs e)
        {
            if (this.pct.Text != "")
                errorProvider1.SetError(this.pct, "");
        }

        private void hw_Validating(object sender, CancelEventArgs e)
        {
            if (this.hw.Text != "")
                errorProvider1.SetError(this.hw, "");
        }

        private void pcb_Validating(object sender, CancelEventArgs e)
        {
            if (this.pcb.Text != "")
                errorProvider1.SetError(this.pcb, "");
        }

        private void pcse_Validating(object sender, CancelEventArgs e)
        {
            if (this.pcse.Text != "")
                errorProvider1.SetError(this.pcse, "");
        }

        private void pcsi_Validating(object sender, CancelEventArgs e)
        {
            if (this.pcsi.Text != "")
                errorProvider1.SetError(this.pcsi, "");
        }

        private void cbrazo_Validating(object sender, CancelEventArgs e)
        {
            if (this.cbrazo.Text != "")
                errorProvider1.SetError(this.cbrazo, "");
        }

        private void ccint_Validating(object sender, CancelEventArgs e)
        {
            if (this.ccint.Text != "")
                errorProvider1.SetError(this.ccint, "");
        }

        private void ccad_Validating(object sender, CancelEventArgs e)
        {
            if (this.ccad.Text != "")
                errorProvider1.SetError(this.ccad, "");
        }

        private void cabd_Validating(object sender, CancelEventArgs e)
        {
            if (this.cabd.Text != "")
                errorProvider1.SetError(this.cabd, "");
        }

        private void cmun_Validating(object sender, CancelEventArgs e)
        {
            if (this.cmun.Text != "")
                errorProvider1.SetError(this.cmun, "");
        }

        private void hcod_Validating(object sender, CancelEventArgs e)
        {
            if (this.hcod.Text != "")
                errorProvider1.SetError(this.hcod, "");
        }

        private void ccod_Validating(object sender, CancelEventArgs e)
        {
            if (this.ccod.Text != "")
                errorProvider1.SetError(this.ccod, "");
        }







        private void button3_Click(object sender, EventArgs e)
        {
            webcam.Start();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            //System.IO.ima tw = new System.IO.StreamWriter("C:../../Fotos/" + dbVic.spVicBuscarIDSiguientePaciente() + ".txt");

            string filename = "../../Fotos/" + (dbVic.spVicBuscarIDSiguientePaciente()+1) + ".jpg";
            FileStream fstream = new FileStream(filename, FileMode.Create);
            imgVideo.Image.Save(fstream, System.Drawing.Imaging.ImageFormat.Jpeg);
            fstream.Close();

            webcam.Stop();
        }

        private void UsuarioAdd_FormClosing(object sender, FormClosingEventArgs e)
        {
            webcam.Stop();
        }
  
    }
}
